package com.winter.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.jiguang.common.utils.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.winter.mapper.DictEntryMapper;
import com.winter.mapper.FriendMapper;
import com.winter.mapper.IcompanypositionMapper;
import com.winter.mapper.UserInfoMapper;
import com.winter.model.AreaCode;
import com.winter.model.DictEntry;
import com.winter.model.Friend;
import com.winter.model.IcompanyCard;
import com.winter.model.UserCompanyBean;
import com.winter.model.UserInfo;
import com.winter.util.Constants;
import com.winter.util.Result;

@Service
public class IcompanypositionService {

    @Resource
    private IcompanypositionMapper icompanypositionMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private FriendMapper friendMapper;
    @Resource
    public DictEntryMapper dictEntryMapper;

    /*
     * 金融圈列表
     */
    public Result icompanyCardList(Integer pageSize, Integer pageNumber, String comClass, String comSubClass, String userNo, String key) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        String isInterMan = null; String datLevel = null;
        if (Strings.isNullOrEmpty(userNo)) {
        	isInterMan = "0";
		}else {
			  UserInfo userInfo = userInfoMapper.selectByUserNo(userNo);
			 if (userInfo != null) {
				 if (userInfo.getIsInterMan().equals("1")) {
					 isInterMan = null;
				 }else {
					 isInterMan = "0";
				 }
				 if (!Strings.isNullOrEmpty(userInfo.getDatLevel())) {
					 datLevel = userInfo.getDatLevel();
				 }
			}
		}
        String orderBy = "";
        List<UserCompanyBean> ic1 = userInfoMapper.finUserInfoList(orderBy, comClass, comSubClass, isInterMan, datLevel, key);
        PageHelper.startPage(pageNumber, pageSize, false);
        List<UserCompanyBean> ic = userInfoMapper.finUserInfoList(orderBy, comClass, comSubClass, isInterMan, datLevel, key);
        // 未登录的情况下不显示数据
        if (Strings.isNullOrEmpty(userNo)) {
            for (UserCompanyBean ub : ic) {
                isShow(ub);
            }
        }
        // 未认证的情况下不显示数据
        if (StringUtils.isNotEmpty(userNo)) {
            UserInfo uit = userInfoMapper.selectByPrimaryKey(Long.parseLong(userNo));
            if(null!=uit){
            	if (uit.getAuditFlg().equals("0")) {
                    for (UserCompanyBean ub : ic) {
                        isShow(ub);
                    }

                }
            }
            
            List<Friend> frList = friendMapper.findFriendList(userNo);
            if (frList.size() > 0) {
                for (UserCompanyBean be : ic) {
                    for (Friend friend : frList) {
                        if (be.getCustId() == Integer.parseInt(friend.getCompanyUser())) {
                            be.setStatus(Integer.parseInt(friend.getStatus()));
                        }
                    }
                }
            }
        }
        PageInfo<UserCompanyBean> pageInfo = new PageInfo<>(ic);
        pageInfo.setTotal(ic1.size());
        return Result.success(pageInfo);
    }

    private void isShow(UserCompanyBean ub) {

        if (null != ub.getUserName()) {
            String na = ub.getUserName()
                          .substring(0, 1) + "***";
            ub.setUserName(na);
        }
        if (null != ub.getCustMobile()) {
            String te = ub.getCustMobile()
                          .substring(0, 3) + "******";
            ub.setCustMobile(te);
        }
        if (null != ub.getCustEmail()) {
            String ma = ub.getCustEmail()
                          .substring(0, 3) + "******";
            ub.setCustEmail(ma);
        }
        String orgEmail = ub.getOrgEmail();
        if (orgEmail != null) {
            String substring = orgEmail.substring(0, 3) + "******";
            ub.setOrgEmail(substring);
        }
        String orgMobile = ub.getOrgMobile();
        if (!Strings.isNullOrEmpty(orgMobile)) {
            ub.setOrgMobile(orgMobile.substring(0, 3) + "******");
        }
    }


    /*
     * 通过id查询金融圈信息
     */
    public Result getIcompany(Long id, String userNo) {
        UserCompanyBean bean = userInfoMapper.getUserCom(id);
        // 未登录的情况下不显示数据
        if (Strings.isNullOrEmpty(userNo)) {
            isShow(bean);
        }
        if (null != bean) {
            if (!Strings.isNullOrEmpty(userNo)) {
                UserInfo uit = userInfoMapper.selectByUserNo(userNo);
                if (uit.getAuditFlg().equals("0")) {
                    isShow(bean);
                }
                Friend friend1 = friendMapper.selectByFriendUser(uit.getCustId() + "", bean.getCustId() + "");
                if (friend1 != null) {
                    bean.setStatus(Integer.valueOf(friend1.getStatus()));
                }
            } else {
                bean.setStatus(0);
            }
            if (!Strings.isNullOrEmpty(bean.getCompanyIndustry())) {
                DictEntry dictEntry1 = dictEntryMapper.get("DC_INDU_CLASSIFY", bean.getCompanyIndustry());
                if (dictEntry1 != null) {
                    bean.setCompanyIndustry(dictEntry1.getDictname());
                }
            }
            String integer = userInfoMapper.boneSum(String.valueOf(bean.getUserNo()));
            String integer1 = userInfoMapper.finSum(String.valueOf(bean.getUserNo()));
            bean.setBoneCount(integer + "");
            bean.setFinCount(integer1 + "");

        }
        getTypeName(bean);
        return Result.success(bean);

    }
    
    public void getTypeName(UserCompanyBean bean) {
        if (!Strings.isNullOrEmpty(bean.getFinClass())) {
        	DictEntry dictEntry4 = dictEntryMapper.get("DC_DJS_ORG_FIN", bean.getFinClass());
        	if(dictEntry4 != null){
        		bean.setFinClassName(dictEntry4.getDictname());
        	}
        	if(!Strings.isNullOrEmpty(bean.getFinSubclass())){
        		if (bean.getFinClass().equals("1")) {
        			DictEntry dictEntry5 = dictEntryMapper.get("DC_DJS_ORG_FIN_BANK", bean.getFinSubclass());
        			if(dictEntry5 != null){
        				bean.setFinClassName(bean.getFinClassName()+"-"+dictEntry5.getDictname());
                	}
    			}else if(bean.getFinClass().equals("6")){
    				DictEntry dictEntry6 = dictEntryMapper.get("DC_DJS_ORG_FIN_FUND", bean.getFinSubclass());
    				if(dictEntry6 != null){
    					bean.setFinClassName(bean.getFinClassName()+"-"+dictEntry6.getDictname());
    	        	}
    			}
        	}
		}
    }

}
